package com.aikon.wht.crawler;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.mongodb.client.FindIterable;
import com.aikon.wht.json.TestOfJson;
import com.aikon.wht.mongo.MongodbPulish;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by haitao.wang on 2016/11/28.
 */
@Component
public class TestOfJsoup {

    @Autowired
    MongodbPulish mongodbPulish;

    public void main(String[] args) throws IOException {
        String url = "https://www.iwjw.com";
        Document doc = Jsoup.connect("https://www.iwjw.com").get();
        Elements eles = doc.select(".wrap-a");
        if (CollectionUtils.isEmpty(eles)) {
            return;
        }
        Set<String> dataCodes = Sets.newLinkedHashSet();
        for (Element ele : eles) {
            String dataCode = ele.attr("data-code");
            if (StringUtils.isBlank(dataCode)) {
                continue;
            }
            dataCodes.add(dataCode);
        }

        Map<String, Map<String,Object>> map2Return = Maps.newHashMap();
        for (String dataCode : dataCodes) {
            String propertyUrl = url + "/estate/new/" + dataCode;
            Map<String, Object> baseInfoMap = Maps.newHashMap();
            Document propertyDoc = Jsoup.connect(propertyUrl).get();
            baseInfoMap.put("name", propertyDoc.select(".base-info-name").get(0).text());
            System.out.println(baseInfoMap.get("name"));
            Elements avgPriceInfos = propertyDoc.select(".price-no");
            Elements totalPriceInfos = propertyDoc.select(".price-pending");
            if (CollectionUtils.isEmpty(avgPriceInfos)) {
                baseInfoMap.put("avgPrice", "");
            } else {
                baseInfoMap.put("avgPrice",avgPriceInfos.get(0).text());
            }
            if (CollectionUtils.isEmpty(totalPriceInfos)) {
                baseInfoMap.put("totalPrice", "");
            } else {
                baseInfoMap.put("totalPrice",totalPriceInfos.get(0).text());
            }

            Elements els = propertyDoc.select("li.i-list > .base-left");
            baseInfoMap.put("openDate", els.get(0).text());
            baseInfoMap.put("serveDate", els.get(1).text());
            baseInfoMap.put("propertyType", els.get(2).text());
            baseInfoMap.put("address", els.get(3).text());
            map2Return.put(dataCode, baseInfoMap);
        }
        String json2Return = TestOfJson.writeValueAsJson(map2Return);
        Map<String, String> sss = new HashedMap();
        sss.put("a", "aa");
        sss.put("b", "bb");
        System.out.println(json2Return);
//        MongodbPulish mongodbPulish = new MongodbPulish();
        mongodbPulish.insertJson(TestOfJson.writeValueAsJson(sss));
        FindIterable<org.bson.Document> fi = mongodbPulish.find();
        for (org.bson.Document fm : fi) {
            System.out.println(TestOfJson.readValueFromJson(fm.toJson(), HashMap.class));
        }
//        Map<String, Map<String,Object>> mapMap = TestOfJson.readValueFromJson(json2Return, HashMap.class);
//        System.out.println(mapMap);



    }

}
